
#pragma hdrstop

#include "TOccupy.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TOccupyItem::TOccupyItem()
{
	CutNumber = -1;
	BeginTime = -999;
	EndTime = -999;
	EnterSpeed = -999;
	LeaveSpeed = -999;
}

bool __fastcall TOccupy::GetOccupy(const int CutNumber)
// Used this funcation for judging state of occupy.
{
	return FindItem(CutNumber) != -1 ? true : false;
}


//---------------------------------------------------------------------------
void __fastcall TOccupy::SetOccupy(const int CutNumber, const bool IsOccupy,
											  const float Time, const float Speed)
//Used this funcation for change state of occupy, and it add or finish the record of occupy.
{
	FOccupy = IsOccupy;
	if (IsOccupy) {
		AddItem(CutNumber, Time, Speed);
	} else {
		FinishItem(CutNumber, Time, Speed);
	}
	return;
}


//---------------------------------------------------------------------------
int __fastcall TOccupy::AddItem(const int CutNumber, const float BeginTime, const float Speed)
{
	TOccupyItem Item;
   int Index = -999;
	if (FindItem(CutNumber) < 0) {
		Item.CutNumber = CutNumber;
		Item.BeginTime = BeginTime;
		Item.EnterSpeed = Speed;
		CutOccupy.push_back(Item);
      Index = CutOccupy.size() + 1;
   }
	return Index;
}

int __fastcall TOccupy::AddItem(const int CutNumber)
{
	TOccupyItem Item;

	Item.CutNumber = CutNumber;
   CutOccupy.push_back(Item);
   return CutOccupy.size();
}

//---------------------------------------------------------------------------
void __fastcall TOccupy::SetLastAxesEnter(const int CutNumber, const float Time, const float Speed)
{
	int i, q;
   q = CutOccupy.size();
	i = FindItem(CutNumber);
   if (i < 0) {
   	i = AddItem(CutNumber);
      CutOccupy[i].LastAxesEnterTime = Time;
      CutOccupy[i].LastAxesEnterSpeed = Speed;
      return;
   }
	if (CutOccupy[i].LastAxesEnterTime > 0.001) return;
   CutOccupy[i].CutNumber == CutNumber;
   CutOccupy[i].LastAxesEnterTime = Time;
   CutOccupy[i].LastAxesEnterSpeed = Speed;
   return;
}

//---------------------------------------------------------------------------
void __fastcall TOccupy::FinishItem(const int CutNumber, const float EndTime, const float Speed)
{
	int i, q;
	q = CutOccupy.size();
	for (i = 0; i < q; i++) {
		if (CutOccupy[i].CutNumber == CutNumber) {
			CutOccupy[i].EndTime = EndTime;
			CutOccupy[i].LeaveSpeed = Speed;
         return;
		}
	}
	return;
}

//---------------------------------------------------------------------------
int __fastcall TOccupy::FindItem(const int CutNumber)
{
	int i, q;
	q = CutOccupy.size();
	for (i = 0; i < q; i++) {
		if (CutOccupy[i].CutNumber == CutNumber) {
			return i;
		}
	}
	return -1;
}

//---------------------------------------------------------------------------
int __fastcall TOccupy::FindLastAxesItem(const int CutNumber)
{
	int i, q;
   q = CutOccupy.size();
   for (i = 0; i < q; i++) {
      if (CutOccupy[i].CutNumber == CutNumber && CutOccupy[i].LastAxesEnterTime > 0) {
         return i;
      }
   }
   return -1;
}

//---------------------------------------------------------------------------
float __fastcall TOccupy::BeginTime(const int CutNumber)
{
	int i, q;
	q = CutOccupy.size();
	for (i = 0; i < q; i++) {
		if (CutOccupy[i].CutNumber == CutNumber) {
			return CutOccupy[i].BeginTime;
		}
	}
	return -1;
}

//---------------------------------------------------------------------------
float __fastcall TOccupy::EndTime(const int CutNumber)
{
	int i, q;
   q = CutOccupy.size();
   for (i = 0; i < q; i++) {
      if (CutOccupy[i].CutNumber == CutNumber) {
         return CutOccupy[i].EndTime;
      }
   }
   return -1;
}

//---------------------------------------------------------------------------
float __fastcall TOccupy::LastAxesEnterTime(const int CutNumber)
{
	int i, q;
   q = CutOccupy.size();
   for (i = 0; i < q; i++) {
      if (CutOccupy[i].CutNumber == CutNumber) {
         return CutOccupy[i].LastAxesEnterTime;
      }
   }
   return -1;
}

//---------------------------------------------------------------------------
void __fastcall TOccupy::ClearOccupyRecord(void)
{
	CutOccupy.clear();
}

//---------------------------------------------------------------------------
float __fastcall TOccupy::GetEnterSpeed(const int CutNumber)
{
	int i;
	for (i = 0; i < CutOccupy.size(); i++) {
		if (CutOccupy[i].CutNumber == CutNumber)
			return CutOccupy[i].EnterSpeed;
	}
	return -999;
}


//---------------------------------------------------------------------------
float __fastcall TOccupy::GetLeaveSpeed(const int CutNumber)
{
	int i;
	for (i = 0; i < CutOccupy.size(); i++) {
		if (CutOccupy[i].CutNumber == CutNumber)
			return CutOccupy[i].LeaveSpeed;
	}
	return -999;
}

